//============================================================================
// Name        : alignment_region.cpp
// Author      : dfoelber
//============================================================================

// INCLUDES //
#include "alignment_region.h"
#include "render_manager.h"
#include "image_manager.h"
#include "draw/SDL_draw.h"
#include "graphics/graphics.h"
#include "util.h"
#include <iostream>
#include <sstream>
//////////////

using namespace std;

/**
* Constructor
**/
AlignmentRegion::AlignmentRegion()
{
	red = blue = yellow = green = white = black = 0;
	redObject = blueObject = yellowObject = greenObject = whiteObject = blackObject = NULL;
}

/**
* Destructor
**/
AlignmentRegion::~AlignmentRegion()
{
}

/**
* Sets the region's boundary.
* 
* Overridden from Region.
* 
* @param x The boundary's X coordinate
* @param y The boundary's Y coordinate
* @param width The boundary's width
* @param height The boundary's height
**/
void AlignmentRegion::SetBoundary( int x, int y, int width, int height )
{
	Region::SetBoundary(x, y, width, height);
	
	if( redObject == NULL )
	{
		Surface* surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
		redObject = new Object( surface, OBJECT_ID_ALIGNMENT, true );
		TooltipInfo* tooltip = new TooltipInfo();
		Graphics::GetGraphics()->CreateStandardTooltip("Fire Alignment", &(tooltip->surface), &(tooltip->background));
		redObject->SetTooltip(tooltip);
		AddObject( redObject );
		
		Color color;
		color.r = 255; color.g = color.b = 0;
		SetAlignmentSurface(redObject, color, red);
	}
	redObject->SetPosition( x, y );
	
	if( blueObject == NULL )
	{
		Surface* surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
		blueObject = new Object( surface, OBJECT_ID_ALIGNMENT, true );
		TooltipInfo* tooltip = new TooltipInfo();
		Graphics::GetGraphics()->CreateStandardTooltip("Water Alignment", &(tooltip->surface), &(tooltip->background));
		blueObject->SetTooltip(tooltip);
		AddObject( blueObject );
		
		Color color;
		color.b = 255; color.g = color.r = 0;
		SetAlignmentSurface(blueObject, color, blue);
	}
	blueObject->SetPosition( x + blueObject->GetPath()->GetWidth() + ALIGNMENT_CONTENT_SPACING, y );
	
	if( yellowObject == NULL )
	{
		Surface* surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
		yellowObject = new Object( surface, OBJECT_ID_ALIGNMENT, true );
		TooltipInfo* tooltip = new TooltipInfo();
		Graphics::GetGraphics()->CreateStandardTooltip("Wind Alignment", &(tooltip->surface), &(tooltip->background));
		yellowObject->SetTooltip(tooltip);
		AddObject( yellowObject );
		
		Color color;
		color.b = 0; color.g = color.r = 255;
		SetAlignmentSurface(yellowObject, color, yellow);
	}
	yellowObject->SetPosition( x, y + yellowObject->GetPath()->GetHeight() + ALIGNMENT_CONTENT_SPACING );
	
	if( greenObject == NULL )
	{
		Surface* surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
		greenObject = new Object( surface, OBJECT_ID_ALIGNMENT, true );
		TooltipInfo* tooltip = new TooltipInfo();
		Graphics::GetGraphics()->CreateStandardTooltip("Earth Alignment", &(tooltip->surface), &(tooltip->background));
		greenObject->SetTooltip(tooltip);
		AddObject( greenObject );
		
		Color color;
		color.g = 255; color.b = color.r = 0;
		SetAlignmentSurface(greenObject, color, green);
	}
	greenObject->SetPosition( x + greenObject->GetPath()->GetWidth() + ALIGNMENT_CONTENT_SPACING, 
							y + greenObject->GetPath()->GetHeight() + ALIGNMENT_CONTENT_SPACING );
	
	if( whiteObject == NULL )
	{
		Surface* surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
		whiteObject = new Object( surface, OBJECT_ID_ALIGNMENT, true );
		TooltipInfo* tooltip = new TooltipInfo();
		Graphics::GetGraphics()->CreateStandardTooltip("Light Alignment", &(tooltip->surface), &(tooltip->background));
		whiteObject->SetTooltip(tooltip);
		AddObject( whiteObject );
		
		Color color;
		color.b = color.g = color.r = 255;
		SetAlignmentSurface(whiteObject, color, white);
	}
	whiteObject->SetPosition( x, y + (whiteObject->GetPath()->GetHeight() + ALIGNMENT_CONTENT_SPACING) * 2 );
	
	if( blackObject == NULL )
	{
		Surface* surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
		blackObject = new Object( surface, OBJECT_ID_ALIGNMENT, true );
		TooltipInfo* tooltip = new TooltipInfo();
		Graphics::GetGraphics()->CreateStandardTooltip("Death Alignment", &(tooltip->surface), &(tooltip->background));
		blackObject->SetTooltip(tooltip);
		AddObject( blackObject );
		
		Color color;
		color.b = color.g = color.r = 0;
		SetAlignmentSurface(blackObject, color, black);
	}
	blackObject->SetPosition( x + blackObject->GetPath()->GetWidth() + ALIGNMENT_CONTENT_SPACING, 
							y + (blackObject->GetPath()->GetHeight() + ALIGNMENT_CONTENT_SPACING) * 2 );
}

/**
* Creates the surface for a given alignment object.
* 
* @param object The alignment object
* @param color The alignment color
* @param value The alignment value
**/
void AlignmentRegion::SetAlignmentSurface(Object* object, Color color, int value)
{
	int tValue = value + 10;
	object->SetBackground(color, (int)(255 * ((double)tValue / 20.0)));
	
	Draw_Rect(object->GetSurface()->GetSurface(), 0, 0, object->GetSurface()->GetWidth(), object->GetSurface()->GetHeight(), color.ToUint32());
	
	ostringstream oss;
	string sign = "";
	if (value > 0) sign = "+";
	if (value < 0) sign = "-";
	oss << sign << (value < 0 ? value * -1 : value);
	
	Color t;
	t.r = t.g = t.b = 0;
	Graphics::GetGraphics()->DrawFont(object->GetSurface(), oss.str(), FONT_DEJA_VU_SANS, FONT_SIZE_NORMAL, t, 
			0, 0, object->GetPath()->GetWidth(), object->GetPath()->GetHeight(), ALIGNMENT_MIDDLE | ALIGNMENT_CENTER );
}

/**
* An effect's state has changed.
* 
* Overridden from EffectsListener
* 
* @param effect The effect that is updated
**/
void AlignmentRegion::EffectStateChanged( Effect* effect )
{
	if( effect->GetId() == EFFECT_ALIGNMENT_CHANGE && effect->GetTrigger() == effect->state )
	{
		vector<string> target = Util::Split( effect->GetTarget(), ',' );
		if( (int)target.size() > 1 && target[1] == EFFECT_TARGET_SELF )
		{
			string action = ((AlignmentChangeEffect*)effect)->GetChange()[effect->level];
			char c = action[0];
			int amount = atoi(action.erase(0, 1).c_str());
			Color color;
			Surface* surface;
			switch( c )
			{
			case 'r':
			case 'R':
				red += amount; if( red < -10 ) red = -10; if( red > 10 ) red = 10;
				color.r = 255; color.g = color.b = 0;
				surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
				redObject->SetSurface(surface, true);
				SetAlignmentSurface(redObject, color, red);
				break;
			case 'b':
			case 'B':
				blue += amount; if( blue < -10 ) blue = -10; if( blue > 10 ) blue = 10;
				color.b = 255; color.g = color.r = 0;
				surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
				blueObject->SetSurface(surface, true);
				SetAlignmentSurface(blueObject, color, blue);
				break;
			case 'g':
			case 'G':
				green += amount; if( green < -10 ) green = -10; if( green > 10 ) green = 10;
				color.g = 255; color.r = color.b = 0;
				surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
				greenObject->SetSurface(surface, true);
				SetAlignmentSurface(greenObject, color, green);
				break;
			case 'y':
			case 'Y':
				yellow += amount; if( yellow < -10 ) yellow = -10; if( yellow > 10 ) yellow = 10;
				color.b = 0; color.g = color.r = 255;
				surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
				yellowObject->SetSurface(surface, true);
				SetAlignmentSurface(yellowObject, color, yellow);
				break;
			case 'w':
			case 'W':
				white += amount; if( white < -10 ) white = -10; if( white > 10 ) white = 10;
				color.r = color.g = color.b = 255;
				surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
				whiteObject->SetSurface(surface, true);
				SetAlignmentSurface(whiteObject, color, white);
				break;
			case 'l':
			case 'L':
				black += amount; if( black < -10 ) black = -10; if( black > 10 ) black = 10;
				color.r = color.g = color.b = 0;
				surface = ImageManager::GetManager()->CreateImage(ALIGNMENT_WIDTH, ALIGNMENT_HEIGHT);
				blackObject->SetSurface(surface, true);
				SetAlignmentSurface(blackObject, color, black);
				break;
			default:
				break;
			}
			
			EffectsManager::GetManager()->ChangeEffectState( effect, EFFECT_STATE_FINISHED );
		}
	}
}
